<?php
error_reporting(E_ALL);
require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// extrage request params de undeva, aici e argv
$requestParams = $argv;

// first param is always the url
$url        = $requestParams[1];

// second param is always the file prefix (file to write to)
$fileName   = $requestParams[2];
$startByte  = $requestParams[3];
$endByte    = $requestParams[4];

// initiate the connection and scan the data
$client = new Zend_Http_Client($url, array(
	'maxredirects' => 0,
	'timeout'      => 30));

try {
	$client->setHeaders('Range', sprintf('bytes=%d-%d', $startByte, $endByte));
	$response = $client->request('GET');

	KChunk_Filesystem::filePutShard($fileName, $startByte, $response->getRawBody());
	echo sprintf('OK : %s', $startByte);
	exit();
} catch (Exception $e) {
	echo 'ERR';
}